﻿// Copyright 2009 William P. Hickey Jr. and William U. Walker
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//     
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;

namespace CSReporter
{
    public class MessageRule : INotifyPropertyChanged
    {
        private int ruleType;
        private int order;
        private bool enabled;
        private string name;
        private string fromCall;
        private string toCall;
        private string digiPath;

        public int RuleType
        {
            get { return ruleType; }
            set {
                ruleType = value;
                this.NotifyPropertyChanged("RuleType");
            }
        }

        public int Order
        {
            get { return order; }
            set {
                order = value;
                this.NotifyPropertyChanged("Order");
            }
        }

        public bool Enabled
        {
            get { return enabled; }
            set
            {
                enabled = value;
                this.NotifyPropertyChanged("Enabled");
            }
        }

        public string Name
        {
            get { return name; }
            set
            {
                name = value == null ? null : value.Substring(0, Math.Min(value.Length, 25)).Trim();
                this.NotifyPropertyChanged("Name");
            }
        }

        public string FromCall
        {
            get { return fromCall; }
            set
            {
                fromCall = value == null ? null : value.Substring(0, Math.Min(value.Length, 10)).Trim().ToUpper();
                this.NotifyPropertyChanged("FromCall");
            }
        }

        public string ToCall
        {
            get { return toCall; }
            set
            {
                toCall = value == null ? null : value.Substring(0, Math.Min(value.Length, 10)).Trim().ToUpper();
                this.NotifyPropertyChanged("ToCall");
            }
        }

        public string DigiPath
        {
            get { return digiPath; }
            set
            {
                digiPath = value == null ? null : value.Substring(0, Math.Min(value.Length, 100)).Trim().ToUpper();
                this.NotifyPropertyChanged("DigiPath");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

    public class MessageRuleList : BindingList<MessageRule>
    {
        public MessageRuleList() { }
            
        public void ReadXml(string fileName)
        {
            if (File.Exists(fileName))
            {
                Clear();
                XmlSerializer deserializer = new XmlSerializer(typeof(List<MessageRule>));
                using (TextReader textReader = new StreamReader(fileName))
                    foreach (MessageRule mr in (List<MessageRule>)deserializer.Deserialize(textReader))
                        Add(mr);
            }
        }

        public void WriteXml(string fileName)
        {
            if (Directory.GetParent(fileName).Exists)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<MessageRule>));
                using (TextWriter textWriter = new StreamWriter(fileName))
                    serializer.Serialize(textWriter, Items);
            }
        }
    }
}
